#include <QMainWindow>
#include <QMenu>
#include <QMenuBar>
#include <QPainter>
#include <QKeyEvent>
#include <QFileDialog>
#include "viewerwindow.h"
#include "iffdecode.h"

/*
	TODO: Replace most things with Qt components
	- Menubar
	- Toggle "Interlace compensation" (2x scale X)
	- Flip X, Y, rotate, inverse
	- + and - keys to inc/decrease size by 10%
	- Update title with deltaX/deltaY?
	- HAM support
	- Save as...
	- AGA support
	- Drag and drop
	- Sidebar with thumbnails when multiple files are opened
*/

static int WindowWidth = 0;
static int WindowHeight = 0;

ViewerWindow::ViewerWindow( char *filename ) {

	/* File menu */

	QAction *openIFF = new QAction( "&Open IFF", this );
	openIFF->setShortcut( QKeySequence( tr("Ctrl+O") ) );
	QAction *saveAs = new QAction(  "&Save as..", this );
	QAction *quit = new QAction ( "&Quit", this );

	QMenu *file;
	file = menuBar()->addMenu( "&File" );
	file->addAction( openIFF );
	file->addAction( saveAs );
	file->addAction( quit );

	/* View menu */

	QAction *viewDouble = new QAction( "&Double size", this );
	viewDouble->setShortcut( QKeySequence( tr( ">" ) ) );
	QAction *viewHalf = new QAction( "&Half size", this );
	viewHalf->setShortcut( QKeySequence( tr( "<" ) ) );
	QAction *viewReset = new QAction( "&Reset view", this );
	viewReset->setShortcut( QKeySequence( tr( "R" ) ) );

	QMenu *view;
	view = menuBar()->addMenu( "&View" );
	view->addAction( viewReset );
	view->addAction( viewDouble );
	view->addAction( viewHalf );

	/* Connect the dots */

	connect( viewDouble, SIGNAL(triggered()), this, SLOT(doubleSize()));
	connect( viewHalf, SIGNAL(triggered()), this, SLOT(halfSize()));
	connect( viewReset, SIGNAL(triggered()), this, SLOT(resetView()));

	connect( openIFF, SIGNAL(triggered()), this, SLOT(openIFF()));
	connect( saveAs, SIGNAL(triggered()), this, SLOT(saveAs()));
	connect( quit, SIGNAL(triggered()), this, SLOT(close()));

	decoder = new IFFDecode();
	if( filename != NULL ) {
		decoder->Load( filename );
		this->setWindowTitle( filename );
		WindowWidth = decoder->GetWidth()*(decoder->IsInterlaced()?2:1);
		WindowHeight = decoder->GetHeight();
		repaint();
	} else {
		WindowWidth = 320;
		WindowHeight = 256;
		this->setWindowTitle( "No file loaded" );
	}
	this->resize( QSize( WindowWidth, WindowHeight ));
}

void ViewerWindow::openIFF() {
	QString name = QFileDialog::getOpenFileName( this, "Open file", "", "IFF files (*.iff);;All files (*.*)" );
	if( ! name.isNull() ) {
		decoder->Load( (char *) name.toAscii().constData() );
		resetView();
	}
}

void ViewerWindow::saveAs() {
	QString name = QFileDialog::getSaveFileName( this, "Save as..", "", "PNG Image (*.png)" );
//	QString name = "omgtest.png";
	decoder->pixmap->save( name, "PNG" );
	printf( "Save As..\n" );
}

void ViewerWindow::paintEvent( QPaintEvent * ) {
	QPainter p(this);
	if( decoder->pixmap != NULL ) {
		double dx = (float)WindowWidth/((float)decoder->GetWidth()); // *(decoder->IsInterlaced()==true?1.0f:0.5f));
		double dy = (float)WindowHeight/(float)decoder->GetHeight();
		p.scale( dx, dy );
		p.drawPixmap( 0, 0, *decoder->pixmap );
	}
}

void ViewerWindow::resetView() {
	WindowWidth = decoder->GetWidth()*(decoder->IsInterlaced()?2:1);
	WindowHeight = decoder->GetHeight();
	// TODO: Also reset window position
	resize( WindowWidth, WindowHeight );
}

void ViewerWindow::resizeEvent( QResizeEvent *e ) {
	WindowWidth = width();
	WindowHeight = height();
}

void ViewerWindow::doubleSize() {
	resize( width()*2, height()*2 );
}

void ViewerWindow::halfSize() {
	resize( width()/2, height()/2 );
}

/*
void ViewerWindow::keyPressEvent( QKeyEvent *e ) {
	switch( e->key() ) {
		case Qt::Key_Escape:
			this->close();
			break;
		case Qt::Key_Greater:	// +100% size (double)
			break;
		case Qt::Key_Less:	// -50% size (half)
			break;
		case Qt::Key_R:		// reset
			resetView();
			break;
	}
}
*/
